
import { copyJson } from '../../common/tool';
import { CardItemType } from './type';
import { cardDirectionType } from '../../common/enum';
import { getStaticState } from '../../state/static-store';
const CARD_STATE = [
  {url: '', val: 0, opacity: 1, isZoom: false, isDestruction: false, isExistancePool: false, type: '0', width: 90, height: 90, top: 0, left: 0, isMask: true, layer: 0, position: ''},
  {url: '', val: 1, opacity: 1, isZoom: false, isDestruction: false, isExistancePool: false, type: '1', width: 90, height: 90, top: 0, left: 0, isMask: true, layer: 0, position: ''},
  {url: '', val: 2, opacity: 1, isZoom: false, isDestruction: false, isExistancePool: false, type: '2', width: 90, height: 90, top: 0, left: 0, isMask: true, layer: 0, position: ''},
  {url: '', val: 3, opacity: 1, isZoom: false, isDestruction: false, isExistancePool: false, type: '3', width: 90, height: 90, top: 0, left: 0, isMask: true, layer: 0, position: ''},
  {url: '', val: 4, opacity: 1, isZoom: false, isDestruction: false, isExistancePool: false, type: '4', width: 90, height: 90, top: 0, left: 0, isMask: true, layer: 0, position: ''},
  {url: '', val: 5, opacity: 1, isZoom: false, isDestruction: false, isExistancePool: false, type: '5', width: 90, height: 90, top: 0, left: 0, isMask: true, layer: 0, position: ''},
  {url: '', val: 6, opacity: 1, isZoom: false, isDestruction: false, isExistancePool: false, type: '6', width: 90, height: 90, top: 0, left: 0, isMask: true, layer: 0, position: ''},
  {url: '', val: 7, opacity: 1, isZoom: false, isDestruction: false, isExistancePool: false, type: '7', width: 90, height: 90, top: 0, left: 0, isMask: true, layer: 0, position: ''},
  {url: '', val: 8, opacity: 1, isZoom: false, isDestruction: false, isExistancePool: false, type: '8', width: 90, height: 90, top: 0, left: 0, isMask: true, layer: 0, position: ''},
  {url: '', val: 9, opacity: 1, isZoom: false, isDestruction: false, isExistancePool: false, type: '9', width: 90, height: 90, top: 0, left: 0, isMask: true, layer: 0, position: ''},
];
const topDeviation = 50
const leftDeviation = 45;
/**创建卡片层级 */
export const crateGroupArray = (list: any[]): any[] => {
  let len = list.length;
  let indexCount = 0;
  const result: any [] = [];
  const twoArray: any[] = [];

  const layerList: number[][] = [];
  let randomLayer = 0
  let iDxNumber = 0;
  while(len > iDxNumber) {
    let randomVal = Math.floor(Math.random() * (49 - 4) + 4);
    twoArray.push(list.slice(iDxNumber, iDxNumber + randomVal))
    iDxNumber += randomVal;
    randomLayer += 1;
  }
  for(let i = 0; i < twoArray.length; i += 1) {
    const row = twoArray[i];
    const layerListchildren: number[] = [];
    indexCount += 1;
    row.map((item, index) => {
      if (item.isExistancePool) {
        return item;
      }
      const randomRow = Math.floor(Math.random() * 13);
      const randomColumn = Math.floor(Math.random() * 13);
      item.left =  ((item.width + 5) / 2) * randomRow + (leftDeviation || 0);
      item.top = ((item.height + 5) / 2) * randomColumn + (topDeviation || 0);
      item.zIndex = indexCount;
      item.layer = i + 1;
      item.index = index;
      item.position = `${i}-${index}`;
      if (i === (twoArray.length - 1)) {
        item.isMask = false;
      }
      layerListchildren.push(item.position);
      return item
    });
    layerList.push(layerListchildren);
    result.push(...row);
  };
  return [result, layerList];
};
/**创建堆卡片 */
const createCardheap = (number: number = 9, diff = 4, eliminate = 3) => {
  const remainder = number % eliminate;
  const quantity = Math.floor(number / eliminate);
  const cardItemList: any[] = [];
  for (let i = 0; i < quantity; i += 1) {
    const difficult = diff >  CARD_STATE.length ?  CARD_STATE.length : diff;
    const random = Math.floor(Math.random() * difficult);
    const item = copyJson(CARD_STATE[random]);
    for(let k = 0; k < eliminate; k += 1){
      cardItemList.push(copyJson(item));
    };
  };
  for (let i = 0; i < remainder; i += 1) {
    const difficult = diff >  CARD_STATE.length ?  CARD_STATE.length : diff;
    const random = Math.floor(Math.random() * difficult);
    const item = copyJson(CARD_STATE[random]);
    cardItemList.push(copyJson(item));
  }
  cardItemList.sort(() => Math.random() - 0.5);
  return crateGroupArray(cardItemList);
}
/**创建行卡片 */
const createCardRow = (number: number, direction: string, initTop: number, indexes: number, diff) => {
  const { screenWidth: SCREEN_WIDTH } = getStaticState('systemInfo');
  const ONE_RPX = getStaticState('oneRpx');
  return Array(number).fill(0).map((_, index) => {
    const difficult = diff > CARD_STATE.length ? CARD_STATE.length : diff;
    const random = Math.floor(Math.random() * difficult);
    const item = copyJson(CARD_STATE[random]);
    const isSizeScreen = getStaticState('isSizeScreen')
    if (item.isExistancePool) {
      return item;
    }
    if (direction === cardDirectionType.left) {
      item.top = initTop + (item.height + item.height / (isSizeScreen ? 4 : 2) ) * indexes + topDeviation;
      item.left = index * 10 + leftDeviation;
      item.position = `${cardDirectionType.left}-${index}`;
      item.zIndex = (index + 1) * 10;
      item.layer = index + 1;
      item.index = index;
      if ((index + 1) === number) {
        item.isMask = false;
      }
    } else {
      item.top = initTop + (item.height + item.height / (isSizeScreen ? 4 : 2) ) * indexes + topDeviation;
      item.position = `${cardDirectionType.right}-${index}`;
      item.left = SCREEN_WIDTH / ONE_RPX  - ((index * 10) + item.width + 5 + leftDeviation);
      item.zIndex = (index + 1) * 10;
      item.layer = index + 1;
      item.index = index;
      if ((index + 1) === number) {
        item.isMask = false;
      }
    }
    return item;
  })
}
const createCardRowAll = (row = 2, rowNumber = 9, initTop = 700, diff = 4) => {
  const tempPlan: Array<CardItemType> = [];
  for(let i = 0; i < row; i += 1) {
    const direction = i % 2 === 0 ? cardDirectionType.left : cardDirectionType.right;
    tempPlan.push(...createCardRow(rowNumber, direction, initTop, i, diff))
  }
  return tempPlan;
};
export const createCardAll = ({head, row, publice = {eliminate: 3}}) => {
  const rowList = createCardRowAll(row.row, row.number, row.initTop, row.diff);
  const [heapList, layerList] = createCardheap(head.number, head.diff, publice.eliminate);
  const cardList = [...heapList, ...rowList];
  return [cardList, layerList];
};
